﻿using IconYuan.Model.DTO.Orders;
using IconYuan.Model.Entity.Markets;
using IconYuan.Model.Entity.Orders;
using IconYuan.Model.Entity.Stores;
using IconYuan.Model.Entity.Users;
using System.Diagnostics;

namespace IconYuan.Api.Services
{
    public class OrdersServices
    {
        public static string GenerateOrderNo()
        {
            var rand = new Random(Guid.NewGuid().GetHashCode());
            return $"{DateTime.Now.ToString("yyMMddHHmmss")}{rand.Next(1000, 9999)}";
        }

        public static Order ToOrder(CheckOrderOut chkOrder, UserAddress userAddress)
        {
            var addressInfo = userAddress != null ? userAddress : new UserAddress();

            return new Order()
            {
                order_status = 20,
                order_no = GenerateOrderNo(),
                user_id = chkOrder.user_id,
                shop_id = chkOrder.shop_id,
                store_id = chkOrder.store_id,

                goods_price = chkOrder.goods_price,
                order_price = chkOrder.order_price,
                pay_price = chkOrder.pay_price,
                points_money = chkOrder.points_money,
                grade_money = chkOrder.grade_money,
                balance_money = chkOrder.balance_money,
                coupon_money = chkOrder.coupon_money,
                coupon_id = chkOrder.coupon_id,
                promotion_id = chkOrder.promotion_id,
                promotion_money = chkOrder.promotion_money,
                points_num = chkOrder.points_num,
                points_gift = chkOrder.points_gift,

                region_id = addressInfo.region_id,
                linkman = addressInfo.name,
                phone = addressInfo.phone,
                address = chkOrder.address,
                buyer_remark = string.Empty,
                delivery_type = chkOrder.delivery_type,
                express_company = string.Empty,
                express_id = 0,
                express_no = string.Empty,
                express_price = chkOrder.express_price,
                express_status = 10,
                express_time = Convert.ToDateTime("1970-01-01"),
                pay_time = Convert.ToDateTime("1970-01-01"),
                pay_type = 20,
                is_settled = chkOrder.parent_id > 0 && chkOrder.pay_price > 0 ? 0 : 2,    //有上级的才需要分佣
                transaction_id = string.Empty,
                create_time = DateTime.Now
            };
        }

        public static OrderDto ToOrderDto(Order order, List<OrderGoods> goodsList, List<Shop> shopList)
        {
            return new OrderDto()
            {
                user_id = order.user_id,
                order_id = order.order_id,
                order_no = order.order_no,
                address = order.address,
                balance_money = order.balance_money,
                coupon_money = order.coupon_money,
                grade_money = order.grade_money,
                promotion_money = order.promotion_money,
                points_money = order.points_money,
                points_num = order.points_num,
                order_price = order.order_price,
                goods_price = order.goods_price,
                pay_price = order.pay_price,
                points_gift = order.points_gift,
                create_time = order.create_time,
                pay_time = order.pay_time,
                linkman = order.linkman,
                phone = order.phone,
                buyer_remark = order.buyer_remark,
                order_status = order.order_status,
                is_settled = order.is_settled,
                status_desc = GetStatusDesc(order.order_status, order.jump),
                goods = goodsList.Where(y => y.order_id == order.order_id).ToList(),
                shop = shopList.Find(y => y.shop_id == order.shop_id),
            };
        }

        /// <summary>
        /// 获取订单状态描述
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
        public static string GetStatusDesc(int orderStatus,int jump)
        {
            //订单状态(10 已取消, 20 待付款, 30 已付款,40 申请退款,41 申请退款退货 ,50 同意退款,60 拒绝退款, 70 已完成)
            if (orderStatus == 30 && jump == 3) return "待兑换";
            if (orderStatus == 70 && jump == 3) return "已兑换";
            switch (orderStatus)
            {
                case 10: return "已取消";
                case 20: return "待付款";
                case 30: return "调理中";
                case 40: return "申请退款";
                case 41: return "申请退款退货";
                case 50: return "同意退款";
                case 60: return "拒绝退款";
                case 70: return "已完成";
            }
            return orderStatus.ToString();
        }

        /// <summary>
        /// 验证订单优惠券
        /// </summary>
        /// <param name="coupon"></param>
        /// <param name="goodsList"></param>
        /// <exception cref="Exception"></exception>    
        public static string CheckCoupon(UserCoupon coupon, List<OrderGoods> goodsList)
        {
            if (coupon == null) return "优惠券不存在";
            if (coupon.is_expire != 0) return "优惠券已过期";
            if (coupon.is_use != 0) return "优惠券已使用";
            if (coupon.range == 20)
            {
                if (goodsList == null) return "缺少订单商品参数";
                if (!goodsList.Exists(x => coupon.range_config.Contains(x.goods_id.ToString())))
                    return "此商品不在优惠券范围";
            }
            return string.Empty;
        }

        //public static void CheckCoupon(UserCoupon coupon, List<OrderGoods> goodsList)
        //{
        //    if (coupon == null) throw new Exception("优惠券不存在");
        //    if (coupon.is_expire != 0) throw new Exception("优惠券已过期");
        //    if (coupon.is_use != 0) throw new Exception("优惠券已使用");
        //    if (coupon.range == 20)
        //    {
        //        if (goodsList == null) throw new Exception("缺少订单商品参数");
        //        if (!goodsList.Exists(x => coupon.range_config.Contains(x.goods_id.ToString())))
        //            throw new Exception("此商品不在优惠券范围");
        //    }

        //}
        /// <summary>
        /// 订单进度状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetTraceDesc(int status)
        {
            switch (status)
            {
                case 20: return "订单等待付款";
                case 30: return "订单已付款";
                case 31: return "订单已发货"; ;
                case 32: return "订单已收货";
                case 40: return "提交了退款不退货申请";
                case 41: return "提交了退款且退货申请";
                case 50: return "商家同意退款";
                case 51: return "商家拒绝退款";
                case 70: return "订单已完成";
            }
            return $"未知状态:{status}";
        }
    }
}
